package com.yaozheng.vocationaltraining.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.RelativeLayout;

import com.yaozheng.vocationaltraining.R;

/**
 * 考试进度按钮状态view (外圆,进度半圆 内图片)
 *
 * @author xiaoqing
 */
public class PKResultProgressButtonStatusView extends RelativeLayout {

    /**
     * 外圆画笔对象
     */
    private Paint outerPaint;
    /**
     * 内圆进度画笔对象
     */
    private Paint insideProgressPaint;
    /**
     * 内圆没进度画笔对象
     */
    private Paint insidePaint;
    /**
     * 内圆画笔对象
     */
    private Paint innerCirclePaint;
    /**
     * 内圆边距
     */
    private float insideMargins;

    /**
     * 当前进度
     */
    private int currProgress;

    int max = 100;

    /**
     * 边框大小
     */
    float outerPaintBorder, progressBorder;


    public PKResultProgressButtonStatusView(Context context, AttributeSet attrs,
                                            int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public PKResultProgressButtonStatusView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public PKResultProgressButtonStatusView(Context context) {
        super(context);
        initView();
    }

    /**
     * 设置当前进度
     *
     * @param currProgress
     */
    public void setCurrProgress(int currProgress) {
        this.currProgress = currProgress;
        invalidate();
    }


    public void initView() {
        outerPaintBorder = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 1, getResources()
                        .getDisplayMetrics());
        progressBorder = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                2, getResources().getDisplayMetrics());
        outerPaint = new Paint();
        outerPaint.setColor(getResources().getColor(
                R.color.exam_menu_outer_border_color));
        outerPaint.setAntiAlias(true);// 设置抗锯齿;;;;
        outerPaint.setStyle(Paint.Style.STROKE);// 设置空心
        outerPaint.setStrokeWidth(outerPaintBorder);
        insidePaint = new Paint();
        insidePaint.setColor(getResources().getColor(
                R.color.progress_not_select_border_color));
        insidePaint.setAntiAlias(true);// 设置抗锯齿;;;;
        insidePaint.setStyle(Paint.Style.STROKE);// 设置空心.
        insidePaint.setStrokeWidth(progressBorder);
        insideProgressPaint = new Paint();
        insideProgressPaint.setColor(getResources().getColor(
                R.color.progress_select_border_color));
        insideProgressPaint.setAntiAlias(true);// 设置抗锯齿;;;;
        insideProgressPaint.setStyle(Paint.Style.STROKE);// 设置空心.
        insideProgressPaint.setStrokeWidth(progressBorder);
        innerCirclePaint = new Paint();
        innerCirclePaint.setColor(Color.parseColor("#e5e5e5"));
        innerCirclePaint.setAntiAlias(true);// 设置抗锯齿;;;;
        innerCirclePaint.setStyle(Paint.Style.FILL_AND_STROKE);// 设置空心.
        insideMargins = getResources().getDimensionPixelSize(
                R.dimen.default_margin)
                + outerPaintBorder + progressBorder;

        setWillNotDraw(false);
        currProgress = 60;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        float width = getWidth() - outerPaintBorder;
//        canvas.drawCircle(getWidth() / 2, getWidth() / 2, (width) / 2,
//                outerPaint);

        /**
         * 弧形
         */
        RectF oval = new RectF(0, 0, getWidth()
                , getWidth() );
        int benginstartAngle = 76;
        /**
         * 红色进度弧形
         */
        canvas.drawArc(oval, 360 * benginstartAngle / max, 360 * currProgress
                / max, false, insideProgressPaint);

        /**
         * 灰色进度弧形
         */
        canvas.drawArc(oval, 360 * (currProgress + benginstartAngle) / max, 360
                * (100 - currProgress) / max, false, insidePaint);
        /**
         * 内圆
         */
        canvas.drawCircle(getWidth() / 2, getWidth() / 2, (width
                 - progressBorder ) / 2, innerCirclePaint);

        super.onDraw(canvas);
    }


    public void setInnerCirclePaintColor(int color) {
        if (innerCirclePaint != null) {
            innerCirclePaint.setColor(color);
        }
    }

    public void setDefaultAllPaintColor() {
        if (outerPaint != null) {
            outerPaint.setColor(getResources().getColor(
                    R.color.exam_menu_outer_border_color));
        }
        if (insideProgressPaint != null) {
            insideProgressPaint.setColor(getResources().getColor(
                    R.color.progress_select_border_color));
        }
        if (insidePaint != null) {
            insidePaint.setColor(getResources().getColor(
                    R.color.progress_not_select_border_color));
        }
        if (innerCirclePaint != null) {
            innerCirclePaint.setColor(Color.parseColor("#e5e5e5"));
        }
    }

    public void setAllPaintColor(int color) {
        if (outerPaint != null) {
            outerPaint.setColor(color);
        }
        if (insideProgressPaint != null) {
            insideProgressPaint.setColor(color);
        }
        if (insidePaint != null) {
            insidePaint.setColor(color);
        }
        if (innerCirclePaint != null) {
            innerCirclePaint.setColor(color);
        }

    }
}
